package com.maimang.platform.common.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类
 *
 * @author Rocky
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

	public final static long ONE_DAY_SECONDS = 86400;
	public final static long ONE_DAY_MILL_SECONDS = 86400000;

	public final static String shortFormat = "yyyyMMdd";
	public final static String shortFormat2 = "MM-dd";
	public final static String longFormat = "yyyyMMddHHmmss";
	public final static String webFormat = "yyyy-MM-dd";
	public final static String timeFormat = "HHmmss";
	public final static String monthFormat = "yyyyMM";
	public final static String chineseDtFormat = "yyyy年MM月dd日";
	public final static String newFormat = "yyyy-MM-dd HH:mm:ss";
	public final static String newFormatLine = "yyyy/MM/dd HH:mm:ss";
	public final static String newFormatShort = "yyyy-MM-dd";
	public final static String newFormatShort2 = "yy-MM-dd";
	public final static String newFormatShort3 = "MM-dd HH:mm";
	public final static String newFormatShort4 = "MM-dd HH:mm:ss";
	public final static String milliFormat = "yyyy-MM-dd HH:mm:ss:SS";
	public final static String noSecondFormat = "yyyy-MM-dd HH:mm";
	public final static String milliSFormat = "yyyy-MM-dd HH:mm:ss:SSS";
	public final static String milliSFormat1 = "yyyyMMddHHmmssSSS";
	public final static String StandardFormat = "yyyy-MM-dd'T'HH:mm:ss";

	public static String YYYY = "yyyy";

	public static String YYYY_MM = "yyyy-MM";

	public static String YYYY_MM_DD = "yyyy-MM-dd";

	public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

	public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	private static String[] parsePatterns = {
			"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
			"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

	public static DateFormat getNewDateFormat(String pattern) {
		DateFormat df = new SimpleDateFormat(pattern);
		df.setLenient(false);
		return df;
	}

	/**
	 * 获取当前Date型日期
	 *
	 * @return Date() 当前日期
	 */
	public static Date getNowDate() {
		return new Date();
	}

	/**
	 * 获取当前日期, 默认格式为yyyy-MM-dd
	 *
	 * @return String
	 */
	public static String getDate() {
		return dateTimeNow(YYYY_MM_DD);
	}

	/**
	 * 获取当前日期, 默认格式为yyyyMMdd
	 *
	 * @return String
	 */
	public static String getDate2() {
		return dateTimeNow(shortFormat);
	}

	public static final String getTime() {
		return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
	}

	public static final String dateTimeNow() {
		return dateTimeNow(YYYYMMDDHHMMSS);
	}

	public static final String dateTimeNow(final String format) {
		return parseDateToStr(format, new Date());
	}

	public static final String dateTime(final Date date) {
		return parseDateToStr(YYYY_MM_DD, date);
	}

	public static final String getShortDate(Date date) {
		return parseDateToStr(shortFormat2, date);
	}


	public static final String parseDateToStr(final String format, final Date date) {
		return new SimpleDateFormat(format).format(date);
	}

	public static final Date dateTime(final String format, final String ts) {
		try {
			return new SimpleDateFormat(format).parse(ts);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 日期路径 即年/月/日 如2018/08/08
	 */
	public static final String datePath() {
		Date now = new Date();
		return DateFormatUtils.format(now, "yyyy/MM/dd");
	}

	/**
	 * @return 今天的日期
	 */
	public static final String getToday() {
		Date now = new Date();
		return DateFormatUtils.format(now, newFormatShort);
	}
	/**
	 * 日期路径 即年/月/日 如20180808
	 */
	public static final String dateTime() {
		Date now = new Date();
		return DateFormatUtils.format(now, "yyyyMMdd");
	}

	/**
	 * 日期型字符串转化为日期 格式
	 */
	public static Date parseDate(Object str) {
		if (str == null) {
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获取服务器启动时间
	 */
	public static Date getServerStartDate() {
		long time = ManagementFactory.getRuntimeMXBean().getStartTime();
		return new Date(time);
	}

	/**
	 * 计算两个时间差
	 */
	public static String getDatePoor(Date endDate, Date nowDate) {
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		// 计算差多少小时
		long hour = diff % nd / nh;
		// 计算差多少分钟
		long min = diff % nd % nh / nm;
		// 计算差多少秒//输出结果
		// long sec = diff % nd % nh % nm / ns;
		return day + "天" + hour + "小时" + min + "分钟";
	}

	/**
	 * 计算距离现在多久，非精确
	 *
	 * @param date
	 * @return
	 */
	public static String getTimeBefore(Date date) {
		Date now = new Date();
		long l = now.getTime() - date.getTime();
		long day = l / (24 * 60 * 60 * 1000);
		long hour = (l / (60 * 60 * 1000) - day * 24);
		long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		String r = "";
		if (day > 0) {
			r += day + "天";
		} else if (hour > 0) {
			r += hour + "小时";
		} else if (min > 0) {
			r += min + "分";
		} else if (s > 0) {
			r += s + "秒";
		}
		r += "前";
		return r;
	}

	/**
	 * 计算距离现在多久，精确
	 *
	 * @param date
	 * @return
	 */
	public static String getTimeBeforeAccurate(Date date) {
		Date now = new Date();
		long l = now.getTime() - date.getTime();
		long day = l / (24 * 60 * 60 * 1000);
		long hour = (l / (60 * 60 * 1000) - day * 24);
		long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		String r = "";
		if (day > 0) {
			r += day + "天";
		}
		if (hour > 0) {
			r += hour + "小时";
		}
		if (min > 0) {
			r += min + "分";
		}
		if (s > 0) {
			r += s + "秒";
		}
		r += "前";
		return r;
	}

	/**
	 * @param dateString 原始日期
	 * @param days       多少天前
	 * @return 多少天前的日期
	 */
	public static String getBeforeDayString(String dateString, int days) {
		Date date;
		DateFormat df = getNewDateFormat(webFormat);

		try {
			date = df.parse(dateString);
		} catch (ParseException e) {
			date = new Date();
		}

		date = new Date(date.getTime() - (ONE_DAY_MILL_SECONDS * days));
		return df.format(date);
	}


	/**
	 * 获取季度开始时间
	 *
	 * @param quarter 季度 1 2 3 4
	 * @return
	 */
	public static DateTime beginOfQuarter(int quarter) {
		Calendar c = Calendar.getInstance();
		if (quarter == 1) {
			c.set(Calendar.MONTH, 0);
		} else if (quarter == 2) {
			c.set(Calendar.MONTH, 3);
		} else if (quarter == 3) {
			c.set(Calendar.MONTH, 6);
		} else if (quarter == 4) {
			c.set(Calendar.MONTH, 9);
		}
		c.set(Calendar.DATE, 1);

		return DateUtil.beginOfQuarter(c.getTime());
	}

	public static void main(String[] args) {
		DateTime dateTime = beginOfQuarter(4);
		System.out.println(parseDateToStr(newFormat, dateTime));
		DateTime end = endOfQuarter(4);
		System.out.println(parseDateToStr(newFormat, end));
		String type = "quarter:1,2,3";
		String quarter = type.substring(type.lastIndexOf(":") + 1);
		System.out.println(quarter);
	}

	/**
	 * 获取季度结束时间
	 *
	 * @param quarter 季度 1 2 3 4
	 * @return
	 */
	public static DateTime endOfQuarter(int quarter) {
		Calendar c = Calendar.getInstance();
		if (quarter == 1) {
			c.set(Calendar.MONTH, 2);
			c.set(Calendar.DATE, 31);
		} else if (quarter == 2) {
			c.set(Calendar.MONTH, 5);
			c.set(Calendar.DATE, 30);
		} else if (quarter == 3) {
			c.set(Calendar.MONTH, 8);
			c.set(Calendar.DATE, 30);
		} else if (quarter == 4) {
			c.set(Calendar.MONTH, 11);
			c.set(Calendar.DATE, 31);
		}
		Date lastDateOfYear = dateTime(webFormat, parseDateToStr(webFormat, c.getTime()) + " 23:59:59");
		return DateUtil.endOfQuarter(lastDateOfYear);
	}

	/**
	 * 获取两个日期之间所有日期
	 *
	 * @param startTime 开始日期
	 * @param endTime   结束日期
	 * @return 返回yyyy-MM-dd的list
	 */
	public static List<String> getDays(String startTime, String endTime, String format) {
		// 返回的日期集合
		List<String> days = new ArrayList<String>();

		DateFormat dateFormat = new SimpleDateFormat(format);
		try {
			Date start = dateFormat.parse(startTime);
			Date end = dateFormat.parse(endTime);

			Calendar tempStart = Calendar.getInstance();
			tempStart.setTime(start);

			Calendar tempEnd = Calendar.getInstance();
			tempEnd.setTime(end);
			tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
			while (tempStart.before(tempEnd)) {
				days.add(dateFormat.format(tempStart.getTime()));
				tempStart.add(Calendar.DAY_OF_YEAR, 1);
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}

		return days;
	}

	/**
	 * 获取两个日期之间所有日期
	 *
	 * @param startTime 开始日期
	 * @param endTime   结束日期
	 * @return 返回yyyy-MM-dd的list
	 */
	public static List<String> getDays(Date startTime, Date endTime) {
		// 返回的日期集合
		List<String> days = new ArrayList<String>();

		DateFormat dateFormat = new SimpleDateFormat(webFormat);
		try {
			Calendar tempStart = Calendar.getInstance();
			tempStart.setTime(startTime);

			Calendar tempEnd = Calendar.getInstance();
			tempEnd.setTime(endTime);
			tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
			while (tempStart.before(tempEnd)) {
				days.add(dateFormat.format(tempStart.getTime()));
				tempStart.add(Calendar.DAY_OF_YEAR, 1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return days;
	}

	/**
	 * 获取两个日期之间所有日期
	 *
	 * @param start 开始日期
	 * @param end   结束日期
	 * @return 返回所有Date的list
	 */
	public static List<Date> getBetweenDates(Date start, Date end) {
		List<Date> result = new ArrayList<Date>();
		Calendar tempStart = Calendar.getInstance();
		tempStart.setTime(start);
		tempStart.add(Calendar.DAY_OF_YEAR, 1);

		Calendar tempEnd = Calendar.getInstance();
		tempEnd.setTime(end);
		while (tempStart.before(tempEnd)) {
			result.add(tempStart.getTime());
			tempStart.add(Calendar.DAY_OF_YEAR, 1);
		}
		return result;
	}
}
